package org.example.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.dao.dao.user.UStudentDao;
import org.example.dao.dao.user.UUserDao;
import org.example.dao.dto.user.UserDto;
import org.example.dao.entity.user.UStudent;
import org.example.dao.entity.user.UUser;
import org.example.dao.dto.user.UserFactory;
import org.example.exception.JwErrorException;
import org.example.exception.ParamLossErrorException;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.commom.StringUtil;
import org.example.skytool.jw.JwUtil;
import org.example.skytool.oss.minio.MinioConstant;
import org.example.skytool.oss.minio.MinioUtil;
import org.example.skytool.mq.rabbitmq.CookieSender;
import org.example.skytool.redis.RedisConstants;
import org.example.skytool.redis.RedisUtils;
import org.example.skytool.security.EncoderUtil;
import org.example.utils.user.RedisUserService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 22449
 */
@Service("userService")
@Slf4j
public class UserServiceImpl {

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private ContextUtil contextUtil;

    @Resource
    private UUserDao uUserDao;

    @Resource
    RedisUserService redisUserService;

    public UserDto getUserInfo() {
        String userId = contextUtil.getId().toString();
        UserDto userDto = redisUserService.getUserDto(userId);
        if (userDto != null) {
            return userDto;
        }
        userDto = uUserDao.selectUserDto(Long.valueOf(userId));
        redisUserService.setUserDto(userDto);
        return userDto;
    }

    public UUser updateNickName(String nickName) {
        Long userId = contextUtil.getId();
        UUser uUser = uUserDao.selectById(userId);
        uUser.setNickName(nickName);
        uUser.setUpdateTime(new Date());
        uUserDao.updateById(uUser);
        redisUserService.delUserDto(userId.toString());
        return uUser;
    }

    public UUser updateIcon(MultipartFile file) {
        Long userId = contextUtil.getId();
        UUser user = uUserDao.selectById(userId);
        String url = minioUtil.upload(file, MinioConstant.USER, userId.toString());
        user.setIcon(url);
        user.setUpdateTime(new Date());
        uUserDao.updateById(user);
        redisUserService.delUserDto(userId.toString());
        return user;
    }

    public String updateSex(String gender) {
        Long userId = contextUtil.getId();
        UUser user = uUserDao.selectById(userId);
        user.setGender(gender);
        user.setUpdateTime(new Date());
        uUserDao.updateById(user);
        redisUserService.delUserDto(userId.toString());
        return gender;
    }

    public String updateSign(String sign) {
        Long userId = contextUtil.getId();
        UUser user = uUserDao.selectById(userId);
        user.setSign(sign);
        user.setUpdateTime(new Date());
        uUserDao.updateById(user);
        redisUserService.delUserDto(userId.toString());
        return sign;
    }

    public void updateAddressDefault(Long address) {
        Long id = contextUtil.getId();
        UUser user = uUserDao.selectById(id);
        user.setDefaultAddress(address);
        user.setUpdateTime(new Date());
        uUserDao.updateById(user);
        redisUserService.delUserDto(id.toString());
    }


}
